import subprocess
import os
import configparser

config = configparser.ConfigParser()
config.read('../Config/config.ini')

engine_path = config.get('EngineInfo', 'engine_path')
project = config.get('ProjectInfo', 'project')
project_path = config.get('ProjectInfo', 'project_path')
project_name = config.get('ProjectInfo', 'project_name')


def get_version():
    with open(project_path + "/" + project_name + '/Config/Version.ini', 'r') as f:
        return f.read().strip()


version = get_version()

choice = input("Build game? (y/n) : ")

if choice == "y":
    if not config.has_section('BuildInfo'):
        config.add_section('BuildInfo')

    clientconfig = input("Select client config to build (Development(0), Debug(1), Shipping(2)) : ")
    if clientconfig == "0":
        config.set('BuildInfo', 'clientconfig', "Development")
        clientconfig = config.get('BuildInfo', 'clientconfig')
    elif clientconfig == "1":
        config.set('BuildInfo', 'clientconfig', "Debug")
        clientconfig = config.get('BuildInfo', 'clientconfig')
    elif clientconfig == "2":
        config.set('BuildInfo', 'clientconfig', "Shipping")
        clientconfig = config.get('BuildInfo', 'clientconfig')

    platform = input("Select platform to build (Win64(0), Linux(1), LinuxArm64(2)) : ")
    if platform == "0":
        config.set('BuildInfo', 'platform', "Win64")
        platform = config.get('BuildInfo', 'platform')
    elif platform == "1":
        config.set('BuildInfo', 'platform', "Linux")
        platform = config.get('BuildInfo', 'platform')
    elif platform == "2":
        config.set('BuildInfo', 'platform', "LinuxArm64")
        platform = config.get('BuildInfo', 'platform')

    buildtype = input("Select build type (Game(0), Client(1), Server(2)) : ")
    if buildtype == "0":
        buildtype = "Game"
        if platform == "0":
            config.set('BuildInfo', 'platform', "Win64")
            platform = config.get('BuildInfo', 'platform')
        elif platform == "1":
            config.set('BuildInfo', 'platform', "Linux")
            platform = config.get('BuildInfo', 'platform')
        elif platform == "2":
            config.set('BuildInfo', 'platform', "LinuxArm64")
            platform = config.get('BuildInfo', 'platform')
    elif buildtype == "1":
        buildtype = "Client"
        if platform == "0":
            config.set('BuildInfo', 'platform', "WindowsClient")
            platform = config.get('BuildInfo', 'platform')
        elif platform == "1":
            config.set('BuildInfo', 'platform', "LinuxClient")
            platform = config.get('BuildInfo', 'platform')
        elif platform == "2":
            config.set('BuildInfo', 'platform', "LinuxArm64Client")
            platform = config.get('BuildInfo', 'platform')
    elif buildtype == "2":
        buildtype = "Server"
        if platform == "0":
            config.set('BuildInfo', 'platform', "WindowsServer")
            platform = config.get('BuildInfo', 'platform')
        elif platform == "1":
            config.set('BuildInfo', 'platform', "LinuxServer")
            platform = config.get('BuildInfo', 'platform')
        elif platform == "2":
            config.set('BuildInfo', 'platform', "LinuxArm64Server")
            platform = config.get('BuildInfo', 'platform')

    archive_directory = f"{project_path}/{project_name}/Builds/{version}/{clientconfig}/{platform}/{buildtype}"


    def build_command(platform, buildtype):
        if buildtype == "Game":
            return [
                f'{engine_path}/Engine/Build/BatchFiles/RunUAT.bat',
                'BuildCookRun',
                '-project={}'.format(project),
                '-noP4',
                '-platform={}'.format(platform),
                '-clientconfig={}'.format(clientconfig),
                '-cook',
                '-game',
                '-allmaps',
                '-build',
                '-stage',
                '-pak',
                '-archive',
                '-archivedirectory={}'.format(archive_directory)
            ]
        elif buildtype == "Client":
            return [
                f'{engine_path}/Engine/Build/BatchFiles/RunUAT.bat',
                'BuildCookRun',
                '-project={}'.format(project),
                '-noP4',
                '-serverplatform={}'.format(platform),
                '-clientconfig={}'.format(clientconfig),
                '-serverconfig={}'.format(clientconfig),
                '-nocompileeditor',
                '-utf8output',
                '-client',
                '-build',
                '-cook',
                '-stage',
                '-pak',
                '-archive',
                '-archivedirectory={}'.format(archive_directory)
            ]
        elif buildtype == "Server":
            return [
                f'{engine_path}/Engine/Build/BatchFiles/RunUAT.bat',
                'BuildCookRun',
                '-project={}'.format(project),
                '-noP4',
                '-serverplatform={}'.format(platform),
                '-clientconfig={}'.format(clientconfig),
                '-serverconfig={}'.format(clientconfig),
                '-nocompileeditor',
                '-utf8output',
                '-server',
                '-build',
                '-cook',
                '-stage',
                '-pak',
                '-archive',
                '-archivedirectory={}'.format(archive_directory)
            ]


    if __name__ == '__main__':
        with open('../Config/config.ini', 'w') as config_file:
            config.write(config_file)

        command = build_command(platform, buildtype)

        subprocess.call(command)

elif choice == "n":
    exit()
else:
    print("Invalid input. Please enter 'y' or 'n'.")
